/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.loof.fonzie;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author <a href="nicolas@apache.org">Nicolas De Loof</a>
 *
 */
public class Criteria
{
    public static Criteria eq( String name, Object value )
    {
        return new Criteria( "e." + name + " = :" + name, name, value );
    }

    public static Criteria notEq( String name, Object value )
    {
        return new Criteria( "e." + name + " != :" + name, name, value );
    }

    public static Criteria lessThan( String name, Object value )
    {
        return new Criteria( "e." + name + " < :" + name, name, value );
    }

    public static Criteria greaterThan( String name, Object value )
    {
        return new Criteria( "e." + name + " > :" + name, name, value );
    }

    public static Criteria lessEquals( String name, Object value )
    {
        return new Criteria( "e." + name + " <= :" + name, name, value );
    }

    public static Criteria greaterEquals( String name, Object value )
    {
        return new Criteria( "e." + name + " >= :" + name, name, value );
    }

    public static Criteria isNull( String name )
    {
        return new Criteria( "e." + name + " is null" );
    }

    public static Criteria isNotNull( String name )
    {
        return new Criteria( "e." + name + " is not null" );
    }

    public static Criteria like( String name, Object value )
    {
        return new Criteria( "e." + name + " like :" + name, name, value );
    }

    public static Criteria between( String name, Object from, Object to )
    {
        return new Criteria( "e." + name + " like :" + name, name + "From", from, name + "To", to );
    }

    private String jpql;

    private Map<String, Object> params = new LinkedHashMap<String, Object>();

    private Criteria( String jpql )
    {
        this.jpql = jpql;
    }

    public boolean isEmpty()
    {
        return jpql == null;
    }

    public String toString()
    {
        return jpql;
    }

    private Criteria( String jpql, String name, Object value )
    {
        if ( value != null )
        {
            this.jpql = jpql;
            this.params.put( name, value );
        }
    }

    private Criteria( String jpql, String name1, Object value1, String name2, Object value2 )
    {
        if ( value1 != null && value2 != null )
        {
            this.jpql = jpql;
            this.params.put( name1, value1 );
            this.params.put( name2, value2 );
        }
    }

    public Map<String, Object> getParams()
    {
        return params;
    }
}
